package com.ymatou.iapi.withdraw.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.withdraw.parameter.PassApprovalBean;
import com.ymatou.iapi.withdraw.parameter.RejectApprovalForHunterBean;
import com.ymatou.iapi.withdraw.parameter.applyAlipayWithDrawForSellerBean;
import com.ymatou.iapi.withdraw.parameter.applyBankWithDrawForBuyerBean;
import com.ymatou.iapi.withdraw.parameter.applyBankWithDrawForHunterBean;
import com.ymatou.iapi.withdraw.parameter.applyBankWithDrawForSellerBean;
import com.ymatou.iapi.withdraw.parameter.applyXloboWithDrawForSellerBean;
import com.ymatou.iapi.withdraw.service.PassApprovalCall;
import com.ymatou.iapi.withdraw.service.PassApprovalForHunterCall;
import com.ymatou.iapi.withdraw.service.RejectApprovalForHunterCall;
import com.ymatou.iapi.withdraw.service.applyAlipayWithDrawForSellerCall;
import com.ymatou.iapi.withdraw.service.applyBankWithDrawForBuyerCall;
import com.ymatou.iapi.withdraw.service.applyBankWithDrawForHunterCall;
import com.ymatou.iapi.withdraw.service.applyBankWithDrawForSellerCall;
import com.ymatou.iapi.withdraw.service.applyXloboWithDrawForSellerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.database.model.withdraw.Ymt_InvoiceFundFlowLogExample;
import com.ymttest.database.model.withdraw.Ymt_InvoiceOperationLog;
import com.ymttest.database.model.withdraw.Ymt_InvoiceOperationLogExample;
import com.ymttest.database.model.withdraw.Ymt_InvoiceSerialSeedExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawIdempotentExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoice;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoiceExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoiceXloboLogExample;
import com.ymttest.database.sqlwapper.WithdrawWapper;
import com.ymttest.utils.TestDataManager;
/**
*审核拒绝--推手提现单运营审核
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RejectApprovalForHunter{
	private static RejectApprovalForHunterBean bean;
	private static RejectApprovalForHunterCall  call;
	Ymt_WithDrawInvoiceExample ymtinvoive=new Ymt_WithDrawInvoiceExample();//提现申请信息主表
	Ymt_InvoiceSerialSeedExample ymtserialseed=new Ymt_InvoiceSerialSeedExample();//单据编号种子表
	Ymt_WithDrawInvoiceXloboLogExample ymtxlobolog=new Ymt_WithDrawInvoiceXloboLogExample();//提现到贝海的日志
	Ymt_InvoiceFundFlowLogExample ymtflowlog=new Ymt_InvoiceFundFlowLogExample();//资金流转操作日志表
	Ymt_InvoiceOperationLogExample ymtoperationlog=new Ymt_InvoiceOperationLogExample();//单据操作日志表
	Ymt_WithDrawIdempotentExample ymtidempotent=new Ymt_WithDrawIdempotentExample();//提现申请幂等键记录表
	
	applyBankWithDrawForHunterBean bean2=new applyBankWithDrawForHunterBean();
	applyBankWithDrawForHunterCall call2=new applyBankWithDrawForHunterCall();
	
	WithdrawWapper WithdrawWapper=new WithdrawWapper();
	
	private final int sellerid=3383;
	private final String sellerName="wesper";
	
	private final int userId=1234; //分销推手Id hunterId  推手没有余额账户，有服务费帐号和佣金帐号
	private final String userName="maggie8315";//推手Name

	private final String fundAccountId="925CF663-FCBA-4380-846B-32162E58C318";
	private final String fundAccountName="吴文推手";
	private final String fundAccountNo="147258369789";
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("审核拒绝--推手提现单运营审核");
	}

	@Before
	public void caseUp() {
		bean=new RejectApprovalForHunterBean();
		 call=new RejectApprovalForHunterCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult( call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	/**
	 * 买手申请到银行卡
	 * @param userId 买家/买手
	 * @throws Exception
	 */
	private  String applyForHunder() throws Exception {
		
		applyBankWithDrawForHunterBean bean=new applyBankWithDrawForHunterBean();
		applyBankWithDrawForHunterCall call=new applyBankWithDrawForHunterCall();
		WithdrawWapper WithdrawWapper=new WithdrawWapper();
		
		//给帐号充值
		String accountid = AccountCallServiceV2.getAccountId(userId, 31);
		AccountCallServiceV2.resetAccount(accountid, new BigDecimal(999999999),
				new BigDecimal(0));
		 
		bean.setUserId(userId);
		bean.setWithDrawChannel(10);
		bean.setAccountTypeId(31);//提现帐户类型Id 31.全球优选-推手销售佣金账户 32.全球优选-管理服务费账户（金牌服务商） 33.全球优选-管理服务费账户（普通服务商）
	    bean.setWithDrawAmount(new BigDecimal(1000));
        bean.setRemark("测试提现到国内银行卡");
        bean.setApplyUserId(userId);
        bean.setApplyLoginId(userName);
        
        bean.setBankAccountName(fundAccountName);
        bean.setBankAccountNo(fundAccountNo);
        bean.setFundAccountId(fundAccountId);
	    bean.setCurrencyType(1);//1 人名币
        bean.setIdempotentNo(new TestDataManager().getRandomNum(8));
        
		call.setData(bean);
		call.callService();	
		return call.getInvoiceId();

	}
	
	
	/**
	 * 
	 * @param invoiceId 单据号
	 * @param invoiceStatus 单据状态 对应InvoiceStatusEnum枚举(-1.失效，0.待处理，1.处理中，2. 处理失败，3.已完成)
	 * @param approveStatus0.等待客服审批（等待一级审批） 1.客服审批拒绝（一级审批拒绝，单据作废） 2.客服审批通过（一级审核通过，等待二级审批）
	 *  3.财务审批拒绝（二级审批拒绝，单据作废） 4.财务审批通过（二级审批通过，等待三级审批） 5.拒绝支付（三级审批拒绝，单据作废） 6.同意支付（三级审批，该单据完成）
	 * @throws InterruptedException
	 */
	private void check(String invoiceId,int invoiceStatus,int approveStatus) throws InterruptedException
	{
		check_WithDrawInvoice(invoiceId,invoiceStatus,approveStatus);
		check_InvoiceOperationLog(invoiceId);
	}
	private void check_WithDrawInvoice(String invoiceId,int invoiceStatus,int approveStatus) throws InterruptedException
	{
		Logger.comment("1:验证数据表 Ymt_WithDrawInvoice 提现申请信息主表");
		Logger.verifyEquals("true", call.getSuccess(), "验证Success");
		Logger.verifyEquals("200", call.getCode(), "验证Code");
		ymtinvoive.createCriteria().andWithDrawInvoiceIdEqualTo(invoiceId);
		List<Ymt_WithDrawInvoice> invoivelist = WithdrawWapper.selectYmtWithDrawInvoice(ymtinvoive);
        if(invoivelist.size()>0)
        {
        	Ymt_WithDrawInvoice invoive=invoivelist.get(0);
        	Logger.verifyEquals(invoiceStatus, invoive.getInvoiceStatus(), "验证Ymt_WithDrawInvoice数据库中 InvoiceStatus字段 是否正确");
        	Logger.verifyEquals(approveStatus, invoive.getApproveStatus(), "验证Ymt_WithDrawInvoice数据库中 ApproveStatus字段 是否正确");
        	Logger.verifyEquals(true, invoive.getApplyTime().toString().length()>0, "验证Ymt_WithDrawInvoice数据库中ApplyTime");
        	Logger.verifyEquals(true, invoive.getUpdateTime().toString().length()>0, "验证Ymt_WithDrawInvoice数据库中UpdateTime");
        }
        else
        {
        	Logger.verifyEquals(false, true, "Ymt_WithDrawInvoice 数据为空，有问题");
        }
	}
	private Ymt_InvoiceOperationLog check_InvoiceOperationLog(String invoiceId) throws InterruptedException
	{
		//2:InvoiceOperationLog 单据操作日志表
		Logger.comment("2:验证数据表 InvoiceOperationLog 单据操作日志表");
		ymtoperationlog.createCriteria().andInvoiceIdEqualTo(invoiceId);
		Ymt_InvoiceOperationLog operationlog=WithdrawWapper.selectYmtInvoiceOperationLog(ymtoperationlog).stream()
				.filter(a->a.getOperatorId().equals(bean.getApprovalUserId())).findFirst().get();
		/**
		 * 1.扣款单 
			2.调解退款单 
			3.充值单 
			4.备用金单 
			5.冻结单 
			6.解冻单 
			7.提现单 
			8.保证金退款单 
			9.自有账户充值单据 
			10.财务调整单据 
			11.商家日账单 
			12.补贴申请单 
			13.财务调整单据 
			14.提现账户 
			15.优惠券回收单据 
			16.发票 
			17.商家月账单 
			18.预充值单
		 */
		Logger.verifyEquals(7, operationlog.getInvoiceType(), "验证数据库Ymt_InvoiceOperationLog 中的字段InvoiceType=7提现单");
		/**
		 *  1.申请 
			2.作废 
			3.审批(客服审批) 
			4.拒绝(客服拒绝) 
			5.财务审批 
			6.财务拒绝 
			7.拒绝支付（三级审批拒绝，单据作废） 
			8.同意支付（三级审批，该单据完成） 
			9.导入成功 
			10.导入失败 
			11.导出成功 
			12.导出失败 
			13.申请解冻 
			14.审批解冻 
			15.拒绝解冻
		 * 
		 */
		Logger.verifyEquals(4, operationlog.getOperationType(), "验证数据库Ymt_InvoiceOperationLog 中的字段OperationType=4.拒绝(客服拒绝)");
        return operationlog;
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectApprovalForHunter_001() {
		Logger.start(true,"正例-审核拒绝--买手提现单运营审核-提现到银行卡-一级审核");
		try {
			String invoiceId=applyForHunder();
			bean.setInvoiceId(invoiceId);
			bean.setApprovalLevel(1);
			bean.setApprovalResult("一级审核_"+TestDataManager.random());
			bean.setApprovalUserId(TestDataManager.getRandomIntNum(5));
			bean.setApprovalUserLoginId("LoginId_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();	
			
			check( invoiceId,-1,1) ;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectApprovalForHunter_003() {
		Logger.start(true,"正例-审核拒绝--买家/买手提现单运营审核-二级审核");
		try {
			PassApprovalBean pbean=new  PassApprovalBean();
			PassApprovalForHunterCall pcall=new PassApprovalForHunterCall();
			String invoiceId=applyForHunder();
			//一级审核
			pbean.setInvoiceId(invoiceId);
			pbean.setApprovalLevel(1);
			pbean.setApprovalResult("一级审核通过_"+TestDataManager.random());
			pbean.setApprovalUserId(TestDataManager.getRandomIntNum(5));
			pbean.setApprovalUserLoginId("LoginId_"+TestDataManager.getRandomIntNum(5));
			pcall.setData(pbean);
			pcall.callService();	
			//二级审核拒绝
			bean.setInvoiceId(invoiceId);
			bean.setApprovalLevel(2);
			bean.setApprovalResult("二级审核拒绝_"+TestDataManager.random());
			bean.setApprovalUserId(TestDataManager.getRandomIntNum(5));
			bean.setApprovalUserLoginId("LoginId_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();	
			
			check_WithDrawInvoice( invoiceId,-1,3) ;	
			
			Logger.comment("2:验证数据表 InvoiceOperationLog 单据操作日志表");
			
			ymtoperationlog.createCriteria().andInvoiceIdEqualTo(invoiceId);
			Optional<Ymt_InvoiceOperationLog>  operationlogList=WithdrawWapper.selectYmtInvoiceOperationLog(ymtoperationlog).stream()
					.filter(a->a.getOperatorId().equals(bean.getApprovalUserId())).findFirst();
			Logger.verifyEquals(7, operationlogList.get().getInvoiceType(), "验证数据库Ymt_InvoiceOperationLog 中的字段InvoiceType=7提现单");
			Logger.verifyEquals(6, operationlogList.get().getOperationType(), "验证数据库Ymt_InvoiceOperationLog 中的字段OperationType=6财务拒绝");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectApprovalForHunter_004() {
		Logger.start(true,"正例-审核拒绝--买家/买手提现单运营审核-三级审核");
		try {
			PassApprovalBean pbean=new  PassApprovalBean();
			PassApprovalForHunterCall pcall=new PassApprovalForHunterCall();
			String invoiceId=applyForHunder();
			//一级审核通过
			pbean.setInvoiceId(invoiceId);
			pbean.setApprovalLevel(1);
			pbean.setApprovalResult("一级审核通过_"+TestDataManager.random());
			pbean.setApprovalUserId(TestDataManager.getRandomIntNum(5));
			pbean.setApprovalUserLoginId("LoginId_"+TestDataManager.getRandomIntNum(5));
			pcall.setData(pbean);
			pcall.callService();
			//二级审核通过
			pbean.setApprovalLevel(2);
			pbean.setApprovalResult("二级审核通过_"+TestDataManager.random());
			pbean.setApprovalUserId(TestDataManager.getRandomIntNum(5));
			pbean.setApprovalUserLoginId("LoginId_"+TestDataManager.getRandomIntNum(5));
			pcall.setData(pbean);
			pcall.callService();
			//二级审核拒绝
			bean.setInvoiceId(invoiceId);
			bean.setApprovalLevel(3);
			bean.setApprovalResult("三级审核拒绝_"+TestDataManager.random());
			bean.setApprovalUserId(TestDataManager.getRandomIntNum(5));
			bean.setApprovalUserLoginId("LoginId_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();	
			
			check_WithDrawInvoice( invoiceId,-1,5) ;	
			
			Logger.comment("2:验证数据表 InvoiceOperationLog 单据操作日志表");
			
			ymtoperationlog.createCriteria().andInvoiceIdEqualTo(invoiceId);
			Optional<Ymt_InvoiceOperationLog>  operationlogList=WithdrawWapper.selectYmtInvoiceOperationLog(ymtoperationlog).stream()
					.filter(a->a.getOperatorId().equals(bean.getApprovalUserId())).findFirst();
			Logger.verifyEquals(7, operationlogList.get().getInvoiceType(), "验证数据库Ymt_InvoiceOperationLog 中的字段InvoiceType=7提现单");
			Logger.verifyEquals(7, operationlogList.get().getOperationType(), "验证数据库Ymt_InvoiceOperationLog 中的字段OperationType=7拒绝支付（三级审批拒绝，单据作废）");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectApprovalForHunter_005() {
		Logger.start(true,"正例-审核拒绝--买家/买手提现单运营审核-invoiceId为空");
		try {
			//String invoiceId=applyForHunder();
			//bean.setInvoiceId(invoiceId);
			bean.setApprovalLevel(1);
			bean.setApprovalResult("审核拒绝_"+TestDataManager.random());
			bean.setApprovalUserId(TestDataManager.getRandomIntNum(5));
			bean.setApprovalUserLoginId("LoginId_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("false", call.getSuccess(), "验证Success");
			Logger.verifyEquals("400", call.getCode(), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectApprovalForHunter_006() {
		Logger.start(true,"正例-审核拒绝--买家/买手提现单运营审核-approvalLevel为空");
		try { 
			String invoiceId=applyForHunder();
			bean.setInvoiceId(invoiceId);
			//bean.setApprovalLevel(1);
			bean.setApprovalResult("一级审核_"+TestDataManager.random());
			bean.setApprovalUserId(TestDataManager.getRandomIntNum(5));
			bean.setApprovalUserLoginId("LoginId_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("false", call.getSuccess(), "验证Success");
			Logger.verifyEquals("400", call.getCode(), "验证Code");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

}